package de.justphil.tcg.tcgserver.game.helpers;

import de.justphil.tcg.tcgserver.game.GameManager;
import de.justphil.tcg.tcgserver.game.domain.Player;
import de.justphil.tcg.tcgserver.websockets.messages.TCGInfoFragment;
import de.justphil.tcg.tcgserver.websockets.util.MessageFactory;
import de.justphil.tcg.tcgserver.websockets.util.MessageWriter;

public class GameReporter {
	
	public interface MessageInjector {
		public String inject(Player player, String msgKey);
	}

	public static void unicast(Player receiver, String json) {
		if (!receiver.isConnected())
			return;
		
		receiver.getSocket().send(json);
	}
	
	public static void broadcast(GameManager gameManager, Player sender, String msgKey, MessageInjector i) {
		for (Player player : gameManager.getPlayers()) {
			if (!player.isConnected())
				continue;
			
			String json = i.inject(player, msgKey);
			
			if (sender == null || !player.equals(sender)) {
				player.getSocket().send(json);
			}
		}
	}
	
	public static void broadcastGameInfo(GameManager gameManager, Player sender, String msgKey) {
		broadcast(gameManager, sender, msgKey, new MessageInjector() {

			@Override
			public String inject(Player player, String msgKey) {
				return MessageWriter.writeJson(MessageFactory.infoMessage(
						msgKey,
						TCGInfoFragment.Type.SERVER_MIDDLEWARE_INFO,
						player.getLocale()
				));
			}
			
		});
	}
	
	public static void broadcastGameInfo(GameManager gameManager, String msgKey, final int type) {
		broadcast(gameManager, null, msgKey, new MessageInjector() {

			@Override
			public String inject(Player player, String msgKey) {
				return MessageWriter.writeJson(MessageFactory.infoMessage(
						msgKey,
						type,
						player.getLocale()
				));
			}
			
		});
	}
	
	public static void broadcastGameProgress(GameManager gameManager, final int s,
			final long pId, final long cId, final int[] pT,
			final int l, final int at, final int d, final int ag) {
		
		broadcast(gameManager, null, null, new MessageInjector() {

			@Override
			public String inject(Player player, String msgKey) {
				return MessageWriter.writeJson(
						MessageFactory.progressMessage(s, pId, cId, pT, l, at, d, ag)
				);
			}
			
		});
	}
	
	public static void broadcastGameProgress(GameManager gameManager, final long winnerId) {
		broadcast(gameManager, null, null, new MessageInjector() {

			@Override
			public String inject(Player player, String msgKey) {
				return MessageWriter.writeJson(
						MessageFactory.progressWinnerMessage(winnerId)
				);
			}
			
		});
	}
	
	public static void broadcastGameProgress(GameManager gameManager, final String command) {
		broadcast(gameManager, null, null, new MessageInjector() {

			@Override
			public String inject(Player player, String msgKey) {
				return MessageWriter.writeJson(
						MessageFactory.progressCommandMessage(command)
				);
			}
			
		});
	}
	
	public static void unicastGameProgress(Player receiver, String cmd, final int slot) {
		unicast(receiver, MessageWriter.writeJson(MessageFactory.progressCommandMessage(cmd, slot)));
	}
	
	public static void unicastGameProgress(Player receiver, String cmd) {
		unicast(receiver, MessageWriter.writeJson(MessageFactory.progressCommandMessage(cmd)));
	}
	
}
